<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>短信发送明细查询</title>
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">

    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.10.2.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <script src="../../js/util.js"></script>
    <script type="text/javascript">

        layui.use(['laypage', 'layer', 'table', 'form', 'jquery', 'laydate', 'element'], function () {
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.$;
            var laydate = layui.laydate;
            var index;
            var mgrNameArr=[];
            var mgrIdArr=[];
            var mgrIdFlag;
            var tableData = {
                elem: "#table",//表格id
                id: "modelReload", //id
                url: '/smsReportDetail/find',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                page: true,
                count: "count",
                limit: 10,
                limits: [10, 20, 30, 40, 50],
                layout: ['prev', 'page', 'next'],
                prev: "上一页",
                next: "下一页",
                cols: [[ //表头
                    {
                        field: 'cityName', title: '地市/区县', width: '5%', align: 'center', templet:function(d){
                    	if('' != d.countyName && null != d.countyName){
                    		return d.cityName+"/"+d.countyName;
                    	}else{
                    		return d.cityName;
                    	}
                    }
                    }
                    , {field: 'mgrName', title: '项目名称', align: 'center'}
                    , {
                        field: 'mgrStateName', title: '项目状态', width: '7%', align: 'center'
                    }
                    , {field: 'regionName', title: '区域', align: 'center'}
                    , {field: 'stationName', title: '基站', align: 'center'}
                    , {field: 'msisdn', title: '用户号码', align: 'center'}
                    , {field: 'userPlace', title: '用户归属省', width: '7%', align: 'center', templet:function(d){
                    	if(d.userPlace=='2000250'){
                    		return "江苏省";
                    	}else{
                    		return "非江苏省";
                    	}
                    }}
                    , {field: 'userCity', title: '用户归属地市', width: '8%', align: 'center', templet:function(d){
                    	if ("11"==d.userCity) {
				            return "苏州市";
				        } else if ("12"==d.userCity) {
				            return "淮安市";
				        } else if ("13"==d.userCity) {
				            return "宿迁市";
				        } else if ("14"==d.userCity) {
				            return "南京市";
				        } else if ("15"==d.userCity) {
				            return "连云港市";
				        } else if ("16"==d.userCity) {
				            return "徐州市";
				        } else if ("17"==d.userCity) {
				            return "常州市";
				        } else if ("18"==d.userCity) {
				            return "镇江市";
				        } else if ("19"==d.userCity) {
				            return "无锡市";
				        } else if ("20"==d.userCity) {
				            return "南通市";
				        } else if ("21"==d.userCity) {
				            return "泰州市";
				        } else if ("22"==d.userCity) {
				            return "盐城市";
				        } else if ("23"==d.userCity) {
				            return "扬州市";
				        } else {
				            return "";
				        }
                    }}
                    , {field: 'sendTime', title: '发送时间', align: 'center'}
                    , {
                        field: 'sendStateName', title: '发送状态', width: '7%', align: 'center'
                    }
                    , {field: 'content', title: '内容', align: 'center'}
                ]], done: function (res) {
                    layer.close(index);
                }
            };
            var source = [];//[{"name":"zhangkun"},{"name":"dashen"}]//["aab","aac","bbd","bbn"];
			
			

            $("#mgrName").keyup(function(){
                var key=$("#mgrName").val();
                var cityId=$("#cityId").val();
                var url = '/util/selectMgrNameLike';
                var param = {
                    cityId: cityId,
                    key:key
                };
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        data:param,
                        success: function (json) {
                            data = json.data;
						    for(var i = 0; i < data.length; i++){
						        source[i] = data[i].name;
						    }
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
				
			});
			
			$("#mgrName").autocomplete({
				source:source,
				messages: {  //message设置不出现匹配关键字的结果
					noResults: '',
					results: function() {}
				},close : function(e, ui){
						var mgrName = $("#mgrName").val();
                		var mgrId=mgrIdArr[mgrNameArr.indexOf(mgrName)];
                		if(!isNull(mgrId)){
                			mgrIdFlag = mgrId;
                			var data = findRegionInfo(mgrId);
			                if (data.length != null) {
			                    var options = '<option value="">请选择</option>';
			                    for (var i = 0; i < data.length; i++) {
			                        options += '<option value="' + data[i].regionId + '">' + data[i].regionName + '</option>';
			                    }
			                    $('#regionId').html(options);
			                    $('#stationId').html('<option value="">请先选择区域</option>');
			                } else {
			                    $('#regionId').html('<option value="">该项目下没有区域信息</option>');
			                }
			                form.render();
                		}
				}
			});
			//根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //根据城市ID查询项目
            function selectMgrInfo(cityId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }

            //根据项目id查询区域
            function findRegionInfo(mgrId) {
                var data;
                var url = '/util/findRegionInfo';
                var param = {
                    mgrId: mgrId,
                    cityId: $("#cityId").val()
                };
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        data: param,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }

            //根据区域查询基站
            function findStationInfo(regionId) {
                var data;
                var url = '/util/findStationInfo';
                var param = {
                    cityId: $("#cityId").val(),
                    regionId: regionId,
                    //mgrId: $("#mgrId").val()
                    mgrId: mgrIdFlag
                };
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        data: param,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }

            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                        mgrNameArr.push(data[i].name);
                        mgrIdArr.push(data[i].id);
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该地市下没有项目</option>');
                }
                $('#regionId').html('<option value="">请先选择项目</option>');
                $('#stationId').html('<option value="">请先选择区域</option>');
                form.render();
            }
          


            //导出
            $("#download").on('click', function () {
                var mgrId = $("#mgrId").val();
                var mgrName = $("#mgrName").val().trim();
                var cityId = $("#cityId").val();
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                var way = $('input:radio:checked').val();
                var month = $("#yearmonth").val();
                var countyId = $("#countyId").val();
                if (cityId == '') {
                    layer.msg('您必须选择一个地市');
                } else if (mgrId == ''&&mgrName == '') {
                    layer.msg('你必须选择一个项目或输入项目简称');
                } else if (!judetmentMonth(startTime, endTime) && way == 0) {
                    layer.msg('您选择的时间段已跨月,请重新选择');
                } else if (way == 1 && month == "") {
                    layer.msg('您必须选择一个月份');
                } else if(mgrName!=""&&IsInArray(mgrNameArr,mgrName)==false){
                     layer.msg('该项目名称不存在或者您没有权限导出该项目');
                }else {
                    if(mgrId==''&&mgrName!=''){
                        mgrId=mgrIdArr[mgrNameArr.indexOf(mgrName)];
                    }
                    var regionId = $("#regionId").val();
                    var stationId = $("#stationId").val();
                    url = '/smsReportDetail/download?mgrId=' + mgrId
                        //+ '&mgrName=' + mgrName
                        + '&cityId=' + cityId
                        + '&startTime=' + startTime
                        + '&endTime=' + endTime
                        + '&way=' + way
                        + '&month=' + month
                        + '&regionId=' + regionId
                        + '&stationId=' + stationId
                        + '&countyId=' + countyId;
                    location.href = url;
                }
            });

            //身份识别
            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        if (json.duty == 1) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(data);
                            form.render();
                        } else if (json.duty == 2) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(data);
                            form.render();
                        } else if (json.duty == 3) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(json.list);
                            form.render();
                        }
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });

            //加载数据表
            $('#query').on('click', function () {
                var mgrId = $("#mgrId").val();
                var mgrName = $("#mgrName").val().trim();
                var receiver = $("#receiver").val().trim();
                //console.info(receiver);
                var cityId = $("#cityId").val();
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                var way = $('input:radio:checked').val();
                var month = $("#yearmonth").val();
                if(mgrId==''&&mgrName!=''){
                   mgrId=mgrIdArr[mgrNameArr.indexOf(mgrName)];
                }
                console.info(mgrId);
                if (cityId == '') {
                    layer.msg('您必须选择一个地市');
                } else if (mgrId == ''&&mgrName=='') {
                    layer.msg('您必须选择一个项目或输入项目简称');
                } else if (!judetmentMonth(startTime, endTime) && way == 0) {
                    layer.msg('对不起，您选择的时间段跨月了，请重新选择');
                } else if (way == 1 && month == "") {
                    layer.msg('您必须选择一个月份');
                } else if(mgrName!=""&&IsInArray(mgrNameArr,mgrName)==false){
                     layer.msg('该项目名称不存在或者您没有权限查看该项目');
                }
                else {
                    index = layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                    table.reload('modelReload', {
                        method: 'post',
                        page: {
                            curr: 1
                        },
                        where: {
                            cityId: $("#cityId").val(),
                            way: $('input:radio:checked').val(),
                            startTime: $("#startTime").val(),
                            endTime: $("#endTime").val(),
                            month: $("#yearmonth").val(),
                            mgrId: mgrId,
                          //  mgrName: $("#mgrName").val(),
                          	receiver: $("#receiver").val().trim(),
                            regionId: $("#regionId").val(),
                            stationId: $("#stationId").val(),
                            countyId: $("#countyId").val()
                        }
                    });
                }
            });
            /* //加载项目
            form.on('select(cityId)', function (data) {
                var data = selectMgrInfo(data.value);
                appendMgr(data);
            }); */
            //监听项目  区县
            form.on('select(cityId)', function (data) {
            	var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data = selectMgrInfo(data.value);
                appendMgr(data);
            });
            //监听项目
            form.on('select(countyId)', function (data) {
            	var data1;
            	if(null!=data.value && ""!=data.value){
            		data1 = selectMgrInfo(data.value);
            	}else{
            		var cityId = $("#cityId").val();
            		data1 = selectMgrInfo(cityId);
            	}
                appendMgr(data1);
            });
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            //加载区域
            form.on('select(mgrId)', function (data) {
				mgrIdFlag = data.value;
                var data = findRegionInfo(data.value);
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].regionId + '">' + data[i].regionName + '</option>';
                    }
                    $('#regionId').html(options);
                    $('#stationId').html('<option value="">请先选择区域</option>');
                } else {
                    $('#regionId').html('<option value="">该项目下没有区域信息</option>');
                }
                form.render();
            });
            //加载基站
            form.on('select(regionId)', function (data) {
                var data = findStationInfo(data.value);
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].stationId + '">' + data[i].stationName + '</option>';
                    }
                    $('#stationId').html(options);
                } else {
                    $('#stationId').html('<option value="">该区域下没有基站信息</option>');
                }
                form.render();
            });
            //时间选择器的样式
            //获取系统时间，做时间选择
            //获取指定月的天数
            var myDate = new Date();
            //当前年份
            var year = myDate.getFullYear();
            //当前月份
            var maxmonth = myDate.getMonth() + 1;
            maxmonth = (maxmonth >= 10 ? maxmonth : 0 + "" + maxmonth);
            //获取当前日期
            var date = myDate.getDate();
            date = (date >= 10 ? date : 0 + "" + date);
            //获取当前小时
            var hour = myDate.getHours();
            hour = (hour >= 10 ? hour : 0 + "" + hour);
            //获取当前分钟
            var min = myDate.getMinutes();
            min = (min >= 10 ? min : 0 + "" + min);
            //当前时间
            var loaclTime = year + '-' + maxmonth + "-" + date + " " + hour + ":" + min + ":" + "00";

            //最大查询的月份
            var maxdate = year + '-' + myDate.getMonth() + '-' + date;

            var minmonth = maxmonth - 5;
            var minyear = (minmonth > 0 ? year : year - 1);
            minmonth = (minmonth > 0 ? minmonth : minmonth + 12);
            //查询的最小月份
            var mindate = minyear + '-' + minmonth + '-' + date;


            //查询的时间段上限
            var maxtime = loaclTime;
            //查询的时间段下限
            var mintime = minyear + '-' + minmonth + "-01" + " 00:00:00";

            //初始化按时间段查询结束时间
            var initiaMaxTime = loaclTime;
            //初始化按时间段查询开始时间
            var initiaMinTime = year + '-' + maxmonth + "-" + date + " 00:00:00";
            //上个月的时间
            //var mm = (myDate.getMonth() > 10 ? myDate.getMonth() : 0 + "" + myDate.getMonth());
            var mm = (myDate.getMonth() > 10 ? myDate.getMonth() : "0" + myDate.getMonth());
            mm = (myDate.getMonth() == 00 ? 12 : mm);
            var initMonth = (myDate.getMonth() == 00 ? year - 1 : year) + "" + mm;

            laydate.render({
                elem: '#yearmonth'
                , type: 'month'
                , format: 'yyyyMM'
                , btns: ['clear', 'confirm']
                , trigger: 'click'
                , value: initMonth
                , max: maxdate
                , min: mindate
            });
            laydate.render({
                elem: '#startTime'
                , type: 'datetime'
                , format: 'yyyy-MM-dd HH:mm:ss'
                , btns: ['clear', 'confirm']
                , trigger: 'click'
                , value: initiaMinTime
                , max: maxtime
                , min: mintime
            });
            laydate.render({
                elem: '#endTime'
                , type: 'datetime'
                , format: 'yyyy-MM-dd HH:mm:ss'
                , trigger: 'click'
                , btns: ['clear', 'confirm']
                , value: initiaMaxTime
                , max: maxtime
                , min: mintime
            });
            table.render(tableData);
           
        });
        function IsInArray(arr,val){ 
            var testStr=','+arr.join(",")+","; 
            return testStr.indexOf(","+val+",")!=-1; 
        } 
        
    </script>
</head>
<body>

<!--nav-->
<div class="layui-html-limit">
    <div class="layui-content-body">
        <div class="scroll-box">
            <div class="breadcrumb-box">
                <span class="layui-breadcrumb" lay-separator="|">
                  <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="">&nbsp;报表发送管理&nbsp;</a>
                  <a href="">&nbsp;短信发送明细查询&nbsp;</a>
                </span>
            </div>

            <div class="content-box">
                <div class="form-select-box">
                    <div class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <!-- <div class="layui-inline">
                                <label class="layui-form-label">省份</label>
                                <div class="layui-input-block">
                                    <input type="text" name="username" lay-verify="required" value="江苏"
                                           autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div> -->
                            <div class="layui-inline">
                                <label class="layui-form-label">地市</label>
                                <div class="layui-input-block">
                                    <select name="cityId" lay-filter="cityId" id="cityId">
                                        <option value="">请选择</option>
                                        <option value="11">苏州</option>
                                        <option value="12">淮安</option>
                                        <option value="13">宿迁</option>
                                        <option value="14">南京</option>
                                        <option value="15">连云港</option>
                                        <option value="16">徐州</option>
                                        <option value="17">常州</option>
                                        <option value="18">镇江</option>
                                        <option value="19">无锡</option>
                                        <option value="20">南通</option>
                                        <option value="21">泰州</option>
                                        <option value="22">盐城</option>
                                        <option value="23">扬州</option>
                                        <option value="25">省级</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
		                            <label class="layui-form-label">区县</label>
		                            <div class="layui-input-block">
		                                <select name="countyId" lay-filter="countyId" id="countyId">
		                                    <option value="">全部</option>
		                                </select>
		                            </div>
		                	</div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">发送时间</label>
                                <div class="layui-inline">
                                    <input type="radio" value="1" title="按月" name="way">
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-input-inline" style="width: 100px;">
                                        <input type="text" id="yearmonth" placeholder="请选择月份" class="layui-input"
                                               readonly>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <input type="radio" value="0" title="发送时间段" name="way" checked="">
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-inline">
                                        <div class="layui-input-inline" style="width: 150px;">
                                            <input type="text" name="date" id="startTime" autocomplete="off"
                                                   class="layui-input" readonly>
                                        </div>
                                        <div class="layui-form-mid">-</div>
                                        <div class="layui-input-inline" style="width: 150px;">
                                            <input type="text" name="date" id="endTime" autocomplete="off"
                                                   class="layui-input" readonly>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-inline" style="color: red">1）短信明细数据只保存6个月,并且不能跨月查询,当前月份不支持按月查询
                                    </br>2） 导出只可以导出2天以前的数据（原因：短信网关48小时内才完全返回短信是否发送成功）
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">项目名称</label>
                                <div class="layui-input-block">
                                    <select name="interest" lay-filter="mgrId" id="mgrId">
                                        <option value="">请先选择地市</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline" id="shortname">
                                <label class="layui-form-label">项目简称</label>
                                <div class="layui-input-block">
                                    <input type="text"  name="mgrName" id="mgrName" lay-verify="mgrName" value="" placeholder="请输入" lay-filter="mgrName"
                                            autocomplete="on" class="layui-input" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">区域选择</label>
                                <div class="layui-input-block">
                                    <select lay-filter="regionId" name="regionId" id="regionId">
                                        <option value="">请先选择项目</option>
                                    </select>
                                </div>
                            </div>
                            <div>
                            <div class="layui-inline">
                                <label class="layui-form-label">基站</label>
                                <div class="layui-input-block">
                                    <select lay-filter="stationId" name="stationId" id="stationId">
                                        <option value="">请先选择区域</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline" id="receiverTel" style="width:297px">
                                <label class="layui-form-label">用户号码</label>
                                <div class="layui-input-block">
                                    <input type="text"  name="receiver" id="receiver"  value="" placeholder="请输入"  class="layui-input" />
                                </div>
                            </div>
                            
                            <div class="layui-inline">
                                <button class="layui-btn layui-btn-normal" id="query">查询</button>
                                <button class="layui-btn layui-btn-normal" type="button" id="download">
			                                            导出
			                    </button>
                                <!-- <div class="layui-inline" style="color: red">正在进行脱敏处理，导出功能暂时无法使用</div> -->
                                <!-- <button class="layui-btn layui-btn-normal" id="download">导出</button> -->
                            </div>
                          </div>
                            
                        </div>
                    </div>
                </div>

                <!--表格-->
                <div class="table-box">
                    <table class="layui-table" id="table">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</body>
</html>